package com.sls.game.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.DownstreamUserDAO;
import com.sls.game.dao.PriceTemplateDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.DownstreamUserInfo;
import com.sls.game.querybean.DownstreamUserQuery;
import com.sls.game.service.DownstreamUserService;

@Service("downstreamUserService")
public class DownstreamUserServiceImpl implements DownstreamUserService {
	private static Logger log = Logger
			.getLogger(DownstreamUserServiceImpl.class);
	@Resource(name = "downstreamUserDAO")
	private DownstreamUserDAO downstreamUserDAO;
	@Resource(name = "priceTemplateDAO")
	private PriceTemplateDAO priceTemplateDAO;
	private Gson gson;

	public DownstreamUserServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public String save(String body, Head head) throws ServerDisponseException {
		DownstreamUserInfo userInfo = gson.fromJson(body,
				DownstreamUserInfo.class);
		this.validate(userInfo);
		this.validateProperty(userInfo);
		DownstreamUserInfo userInfo2 = null;
		try {
			userInfo2 = this.downstreamUserDAO.queryUserByUserNO(userInfo
					.getUserNo());
		} catch (Exception e1) {
			log.error(e1.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		if (userInfo2 != null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.DOWN_STREAM_USER_EXIST.getCode(),
					ExceptionCodeDes.DOWN_STREAM_USER_EXIST.getDescription());
		}
		try {
			String templateCode = queryTemplateCodeByName(userInfo);
			userInfo.setId(UUID.randomUUID().toString());
			userInfo.setInsertTime(ServerDateAndTime.getNowDateTimeOfFormat());
			userInfo.setTemplateCode(templateCode);
			
			this.downstreamUserDAO.insert(userInfo);
			return null;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	/**
	 * 通过模板名称查询模板编号
	 * 
	 * @param userInfo
	 * @return
	 */
	private String queryTemplateCodeByName(DownstreamUserInfo userInfo) {
		try {
			String templateCode = this.priceTemplateDAO
					.queryTemplateCodeByName(userInfo.getTemplateName().trim());
			return templateCode;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public String update(String body, Head head) throws ServerDisponseException {
		DownstreamUserInfo userInfo = gson.fromJson(body,
				DownstreamUserInfo.class);
		this.validate(userInfo);
		this.validateProperty(userInfo);
		try {
			String templateCode = this.priceTemplateDAO
					.queryTemplateCodeByName(userInfo.getTemplateName());
			userInfo.setTemplateCode(templateCode);
			userInfo.setModifyTime(ServerDateAndTime.getNowDateTimeOfFormat());
			this.downstreamUserDAO.update(userInfo);
			return null;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public String queryUserByUserNO(String body, Head head)
			throws ServerDisponseException {
		DownstreamUserInfo userInfo = gson.fromJson(body,
				DownstreamUserInfo.class);
		this.validate(userInfo);
		if (StringUtils.isEmpty(userInfo.getUserNo())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.DOWN_STREAM_USER_ACCOUNT.getCode(),
					ExceptionCodeDes.DOWN_STREAM_USER_ACCOUNT.getDescription());
		}
		try {
			userInfo = this.downstreamUserDAO.queryUserByUserNO(userInfo
					.getUserNo());
			if (userInfo != null) {
				return gson.toJson(userInfo);
			}
			return null;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public String queryUserById(String body, Head head)
			throws ServerDisponseException {
		DownstreamUserInfo userInfo = gson.fromJson(body,
				DownstreamUserInfo.class);
		this.validate(userInfo);
		try {
			userInfo = this.downstreamUserDAO.queryUserById(userInfo.getId());
			if (userInfo != null) {
				return gson.toJson(userInfo);
			} else
				return null;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public String queryDownstreamUser(String body, Head head)
			throws ServerDisponseException {
		DownstreamUserQuery userQuery = gson.fromJson(body,
				DownstreamUserQuery.class);
		this.validate(userQuery);
		try {
			Pagination<DownstreamUserInfo> page = this.downstreamUserDAO
					.queryDownstreamUser(userQuery);
			return gson.toJson(page);
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	/**
	 * 校验是否为null
	 * 
	 * @param obj
	 * @throws ServerDisponseException
	 */
	private void validate(Object obj) throws ServerDisponseException {
		if (obj == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
	}

	/**
	 * 校验属性是否为空
	 * 
	 * @param userInfo
	 * @throws ServerDisponseException
	 */
	private void validateProperty(DownstreamUserInfo userInfo)
			throws ServerDisponseException {
		if (StringUtils.isEmpty(userInfo.getUserNo())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.DOWN_STREAM_USER_ACCOUNT.getCode(),
					ExceptionCodeDes.DOWN_STREAM_USER_ACCOUNT.getDescription());
		} else if (StringUtils.isEmpty(userInfo.getUserName())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.DOWN_STREAM_USER_NAME.getCode(),
					ExceptionCodeDes.DOWN_STREAM_USER_NAME.getDescription());
		}  /*else if (StringUtils.isEmpty(userInfo.getTemplateName())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.TEMPLATE_NAME_EMPTY.getCode(),
					ExceptionCodeDes.TEMPLATE_NAME_EMPTY.getDescription());
		}*/ else if (StringUtils.isEmpty(userInfo.getSignTime())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.DOWN_STREAM_USER_SIGN_EMPTY.getCode(),
					ExceptionCodeDes.DOWN_STREAM_USER_SIGN_EMPTY
							.getDescription());
		} else if (StringUtils.isEmpty(userInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		} else {

		}

	}

	/**
	 * 下游用户密钥查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String querySecretKey(String body, Head head)
			throws ServerDisponseException {
		DownstreamUserInfo userInfo = gson.fromJson(body,
				DownstreamUserInfo.class);
		boolean flag = checkDataOfQuerySecretKey(userInfo);
		if (flag) {
			String secretKey = null;
			secretKey = downstreamUserDAO.queryUserSecretKey(userInfo);
			if (secretKey != null && !("".equals(secretKey))) {
				return secretKey;
			}
		}

		return null;
	}

	/**
	 * 数据校验(下游用户密钥查询)
	 * 
	 * @param userInfo
	 * @return
	 */
	private boolean checkDataOfQuerySecretKey(DownstreamUserInfo userInfo) {
		boolean flag = false;
		String userNo = null;
		if (userInfo != null) {
			userNo = userInfo.getUserNo();
		}

		if (userNo == null || "".equals(userNo)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.DOWN_STREAM_USER_ACCOUNT.getCode(),
					ExceptionCodeDes.DOWN_STREAM_USER_ACCOUNT.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

}
